@echo off
chcp 65001 >nul
title Unit Choice Quiz System - Startup
color 0A

echo.
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                Unit Choice Quiz System                       ║
echo ║                    Enhanced Startup                          ║
echo ╚══════════════════════════════════════════════════════════════╝
echo.

REM Kill existing processes to avoid port conflicts
echo [INFO] Checking for existing processes...
taskkill /f /im node.exe >nul 2>&1
taskkill /f /im python.exe >nul 2>&1
timeout /t 2 /nobreak >nul

REM Check if Python is available
echo [INFO] Checking Python installation...
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] ❌ Python not found, please install Python first
    echo [INFO] Download from: https://www.python.org/downloads/
    pause
    exit /b 1
)
for /f "tokens=2" %%i in ('python --version 2^>^&1') do set PYTHON_VERSION=%%i
echo [INFO] ✅ Python %PYTHON_VERSION% found

REM Check if Node.js is available
echo [INFO] Checking Node.js installation...
node --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] ❌ Node.js not found, please install Node.js first
    echo [INFO] Download from: https://nodejs.org/
    pause
    exit /b 1
)
for /f "tokens=1" %%i in ('node --version 2^>^&1') do set NODE_VERSION=%%i
echo [INFO] ✅ Node.js %NODE_VERSION% found

REM Check if required directories exist
if not exist "backend" (
    echo [ERROR] ❌ Backend directory not found
    pause
    exit /b 1
)
if not exist "frontend" (
    echo [ERROR] ❌ Frontend directory not found
    pause
    exit /b 1
)

echo.
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                    Installing Dependencies                   ║
echo ╚══════════════════════════════════════════════════════════════╝

REM Install backend dependencies
echo [INFO] 📦 Installing backend dependencies...
cd backend
pip install -r requirements.txt >nul 2>&1
if errorlevel 1 (
    echo [ERROR] ❌ Failed to install backend dependencies
    echo [INFO] Please check your Python installation and try again
    pause
    exit /b 1
)
echo [INFO] ✅ Backend dependencies installed successfully
cd ..

REM Install frontend dependencies
echo [INFO] 📦 Installing frontend dependencies...
cd frontend
npm install >nul 2>&1
if errorlevel 1 (
    echo [ERROR] ❌ Failed to install frontend dependencies
    echo [INFO] Please check your Node.js installation and try again
    pause
    exit /b 1
)
echo [INFO] ✅ Frontend dependencies installed successfully
cd ..

echo.
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                     Starting Services                       ║
echo ╚══════════════════════════════════════════════════════════════╝

REM Start backend server
echo [INFO] 🚀 Starting backend API server (port 8000)...
start "🐍 Backend Server - FastAPI" cmd /k "title Backend Server ^& color 0E ^& cd /d "%~dp0backend" ^& echo Starting backend server... ^& echo Backend will be available at: http://localhost:8000 ^& echo Admin panel: http://localhost:8000/admin ^& echo. ^& python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload"

REM Wait for backend to start
echo [INFO] ⏳ Waiting for backend server to start...
timeout /t 8 /nobreak >nul

REM Start frontend development server
echo [INFO] 🚀 Starting frontend development server (port 3000)...
start "⚛️ Frontend Server - Vite" cmd /k "title Frontend Server ^& color 0B ^& cd /d "%~dp0frontend" ^& echo Starting frontend development server... ^& echo Frontend will be available at: http://localhost:3000 ^& echo Please wait, this may take a moment... ^& echo. ^& npm run dev"

REM Wait for frontend to start
echo [INFO] ⏳ Waiting for frontend server to start...
timeout /t 15 /nobreak >nul

echo.
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                    Service Status Check                     ║
echo ╚══════════════════════════════════════════════════════════════╝

REM Check backend status
echo [INFO] 🔍 Testing backend connection...
curl -s http://localhost:8000/api/questions >nul 2>&1
if errorlevel 1 (
    echo [WARNING] ⚠️ Backend may not be fully started yet
    echo [INFO] Please check the "Backend Server" window
) else (
    echo [INFO] ✅ Backend is accessible at http://localhost:8000
)

REM Check frontend status
echo [INFO] 🔍 Testing frontend connection...
curl -s http://localhost:3000 >nul 2>&1
if errorlevel 1 (
    echo [WARNING] ⚠️ Frontend may not be fully started yet
    echo [INFO] Please check the "Frontend Server" window
    echo [INFO] Look for "Local: http://localhost:3000" message
) else (
    echo [INFO] ✅ Frontend is accessible at http://localhost:3000
)

echo.
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                      Startup Complete!                      ║
echo ╚══════════════════════════════════════════════════════════════╝
echo.
echo 🎉 Services Status:
echo   📱 Frontend: http://localhost:3000
echo   🔧 Backend:  http://localhost:8000  
echo   👨‍💼 Admin:    http://localhost:8000/admin
echo.
echo 📋 Two server windows should be running:
echo   1. 🐍 Backend Server (Python/FastAPI) - Yellow
echo   2. ⚛️ Frontend Server (Node.js/Vite) - Cyan
echo.
echo 💡 Development Tips:
echo   • Wait for "Local: http://localhost:3000" in Frontend window
echo   • Both servers support hot reload for development
echo   • Close server windows to stop services
echo   • Use Ctrl+C in server windows to stop gracefully
echo.
echo Press any key to open browser...
pause >nul

REM Open browser
echo [INFO] 🌐 Opening browser...
start http://localhost:3000

echo.
echo ✅ Browser opened! Happy coding! 🚀
echo.
echo 📝 Note: This startup window can be closed safely.
echo    The servers will continue running in their own windows.
echo.
echo Press any key to exit...
pause >nul
